package com.guli.product.app;

import com.guli.common.constant.RedisConstant;
import com.guli.common.utils.R;
import com.guli.common.validation.AddValidation;
import com.guli.common.validation.UpdateValidation;
import com.guli.product.entity.CategoryEntity;
import com.guli.product.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;


/**
 * 商品三级分类
 * @author zzb
 * &#064;email  1626131069@qq.com
 * &#064;date  2023-10-21 10:18:56
 */
@RestController
@RequestMapping("product/category")
@Api
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询商品分类
     * @return 返回商品三级分类信息
     */
    @GetMapping("/list/tree")
    @ApiOperation(value = "查询商品分类")
    public R listWithTree() {
        List<CategoryEntity> categoryList = categoryService.listWithTree();
        return R.ok().put("data", categoryList);
    }

    /**
     * 保存商品分类信息
     * @param categoryEntity {
     * "name": "网络书刊",
     * "parentCid": 22,
     * "catLevel": 3,
     * "showStatus": 1,
     * "sort": 0,
     * "productUnit": "",
     * "icon": "",
     * "catId": null
     * }
     * @return
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存商品分类信息")
    public R saveSort(@Validated(value = AddValidation.class) @RequestBody CategoryEntity categoryEntity) {
        boolean save = categoryService.save(categoryEntity);
        if (save) {
            redisTemplate.delete(RedisConstant.CATEGORY_LIST);
            return R.ok();
        }
        return R.error("保存失败");
    }

    /**
     * 删除商品分类信息
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除商品分类信息")
    public R deleteCatrgories(@RequestBody Long[] ids) {
        categoryService.removeByIdsWithRelation(Arrays.asList(ids));
        redisTemplate.delete(RedisConstant.CATEGORY_LIST);
        return R.ok();
    }

    /**
     * 根据catId获取商品分类信息
     * @param catId
     * @return
     */
    @GetMapping("info/{catId}")
    @ApiOperation(value = "根据catId获取商品分类信息")
    public R getCategoryById(@PathVariable Long catId) {
        CategoryEntity categoryEntity = categoryService.getById(catId);
        return R.ok().put("data", categoryEntity);
    }

    /**
     * 修改分类父子关系以及顺序
     * @param categoryEntity "catId": 0,
     * "catLevel": 0,
     * "parentCid": 0,
     * "sort": 0
     * @return
     */
    @PostMapping("/update/sort")
    @ApiOperation(value = "修改分类父子关系以及顺序")
    public R updateSort(@Validated(value = UpdateValidation.class) @RequestBody CategoryEntity[] categoryEntity) {
        categoryService.updateBatchById(Arrays.asList(categoryEntity));
        redisTemplate.delete(RedisConstant.CATEGORY_LIST);
        return R.ok();
    }

    /**
     * 更新商品分类信息
     * @param categoryEntity
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "更新商品分类信息")
    public R updateCategory(@Validated(value = UpdateValidation.class) @RequestBody CategoryEntity categoryEntity) {
        categoryService.updateRelationCategory(categoryEntity);
        redisTemplate.delete(RedisConstant.CATEGORY_LIST);
        return R.ok();
    }
}
